//
//  KFLLaywerTalkRequest.m
//  KWNLaywerClient
//
//  Created by 孔六五 on 15/1/22.
//  Copyright (c) 2015年 张明磊. All rights reserved.
//

#import "KFLLaywerTalkRequest.h"
#import "Model_questionDetailNew.h"
#import "Model_QuestionComments.h"
#import "Model_message.h"

@implementation KFLLaywerTalkRequest
@synthesize laywerTalk_content;
@synthesize laywerTalk_content_img;
@synthesize laywerTalk_content_speech;
@synthesize laywerTalk_qId;


- (id)initRequest
{
    self = [super initRequest];
    if (self) {
        self.laywerTalk_content_img = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)startToRequestWithBlock:(requestResultBlock)resultBlock;
{
    self.kflEngine.hostName = @"lawyanswquestion";

    NSString *string_uid = [[Model_user sharedInstance] getUserID];
    NSString *string_token = [[Model_user sharedInstance] getToken];
    
    NSMutableArray *array_sort = [NSMutableArray arrayWithObjects:@"lawyanswquestion",string_uid,laywerTalk_qId,string_token,Model,Version,nil];
    NSString *string_sign = [self getSignWithArray:array_sort];
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:@"lawyanswquestion" forKey:@"method"];
    [dic setObject:string_uid forKey:@"id"];
    [dic setObject:laywerTalk_qId forKey:@"question_id"];
    [dic setObject:laywerTalk_content forKey:@"content"];
    [dic setObject:@"" forKey:@"content_img1"];
    [dic setObject:@"" forKey:@"content_img2"];
    [dic setObject:@"" forKey:@"content_speech"];
    [dic setObject:string_token forKey:@"token"];
    [dic setObject:Model forKey:@"src"];
    [dic setObject:Version forKey:@"edition"];
    [dic setObject:string_sign forKey:@"sig"];
    
    NSString *string_url = [KWNBaseURL_POST stringByAppendingString:@"lawyer/lawyanswquestion"];

    MKNetworkOperation* op = [self.kflEngine operationWithURLString:string_url params:dic httpMethod:@"POST"];
    if(laywerTalk_content_img){
        if(laywerTalk_content_img.count == 1){
            NSString *path = laywerTalk_content_img[0];
            [op addFile:path forKey:@"content_img1" mimeType:@"image/png"];
        }
        else if(laywerTalk_content_img.count == 2){
            NSString *path1 = laywerTalk_content_img[0];
            NSString *path2 = laywerTalk_content_img[1];
            [op addFile:path1 forKey:@"content_img1" mimeType:@"image/png"];
            [op addFile:path2 forKey:@"content_img2" mimeType:@"image/png"];
        }
    }

    [op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSError *error;
            
            NSDictionary *kindDic = [NSJSONSerialization JSONObjectWithData:completedOperation.responseData options:NSJSONReadingMutableLeaves error:&error];
            
            NSNumber *number_code = [kindDic objectForKey:@"code"];
            NSString *string_message = [kindDic objectForKey:@"msg"];
            NSMutableArray *array_result = [NSMutableArray array];
            
            if(!number_code || !string_message){
                [array_result addObject:@"系统发生错误"];
            }
            else{
                if (number_code.integerValue == 10000) {
                    NSDictionary *dic_data = [kindDic objectForKey:@"data"];
                    array_result = [self returnMessageDataArr:dic_data];
                }
                else{
                    [array_result addObject:string_message];
                }
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                resultBlock(number_code.integerValue,array_result);
            });
        });
    }errorHandler:^(MKNetworkOperation *completedOperation,NSError *error) {
        NSMutableArray *array_data = [NSMutableArray arrayWithObject:@"发送失败"];
        resultBlock(10001,array_data);
    }];
    [self.kflEngine enqueueOperation:op];
}

- (NSMutableArray *)returnMessageDataArr:(NSDictionary *)dic
{
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    Model_questionDetailNew *model_questDetail = [[Model_questionDetailNew alloc] init];
    Model_QuestionComments *model_questComment = [[Model_QuestionComments alloc] init];
    
    NSString *qid = [dic objectForKey:@"id"];
    NSString *question = [dic objectForKey:@"question"];
    NSString *question_area = [dic objectForKey:@"question_area"];
    NSString *time = [dic objectForKey:@"time"];
    NSString *islawyansw = [dic objectForKey:@"islawyansw"];
    NSArray *array_img = [dic objectForKey:@"question_img"];
    NSArray *array_img_small = [dic objectForKey:@"img_thumbnail"];
    NSDictionary *questCommentDic = [dic objectForKey:@"comment"];
    
    if(qid && ![qid isKindOfClass:[NSNull class]]){
        model_questDetail.string_id = qid;
    }
    if(question && ![question isKindOfClass:[NSNull class]]){
        model_questDetail.string_question = question;
    }
    if(question_area && ![question_area isKindOfClass:[NSNull class]]){
        model_questDetail.string_area = question_area;
    }
    if(time && ![time isKindOfClass:[NSNull class]]){
        model_questDetail.string_time = time;
    }
    if(islawyansw && ![islawyansw isKindOfClass:[NSNull class]]){
        model_questDetail.string_islawyansw = islawyansw;
    }
    if(array_img && ![array_img isKindOfClass:[NSNull class]]){
        model_questDetail.arr_img = array_img;
    }
    if(array_img_small && ![array_img_small isKindOfClass:[NSNull class]]){
        model_questDetail.arr_thumbnail = array_img_small;
    }
    
    if(questCommentDic && ![questCommentDic isKindOfClass:[NSNull class]]){
        [model_questComment getQuestionCommentWithDic:questCommentDic];
        model_questDetail.model_Comments = model_questComment;
    }
    else
    {
        model_questDetail.model_Comments = nil;
    }
    
    //计算高度
    [model_questDetail heightWithData];
    [arr addObject:model_questDetail];
    
    NSArray *arr_message = [dic objectForKey:@"questiondetails"];
    if(arr_message && ![arr_message isKindOfClass:[NSNull class]]){
        for (int i = 0; i < arr_message.count; i ++) {
            NSDictionary *message_dic = [arr_message objectAtIndex:i];
            
            Model_message *message = [[Model_message alloc] init];
            [message getModel_messageWithDic:message_dic];
            [arr addObject:message];
        }
    }
    
    return arr;
}


@end
